package com.ptszyxx.popose.module.main.mine;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.BuildConfig;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.utils.DataCleanManager;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ysg.utils.YChannelUtil;
import com.ptszyxx.popose.common.utils.YLoginUtil;
import com.ysg.http.data.AppViewModelFactory;
import com.ptszyxx.popose.databinding.FragmentMineSetBinding;
import com.ptszyxx.popose.module.main.mine.vm.MineSetVM;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.entity.mine.VersionEntity;
import com.ysg.utils.YDialogUtil;
import com.ysg.utils.YSPUtils;
import com.ysg.utils.YStringUtil;

public class MineSetFragment extends BaseFragment<FragmentMineSetBinding, MineSetVM> {
    public static MineSetFragment getInstance() {
        return new MineSetFragment();
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_mine_set;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public MineSetVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(MineSetVM.class);
    }

    @Override
    public void initView() {
        super.initView();
        setCacheUI();
        binding.tvVersion.setContent("V"+ BuildConfig.VERSION_NAME);
        if (YStringUtil.eq(2, YSPUtils.getInstance().getUserSex())){
            binding.tvPriceSet.setVisibility(View.VISIBLE);
        }
        if (YChannelUtil.getInstance().vivo()){
            binding.tvPush.setVisibility(View.VISIBLE);
            binding.tvPush.setSwitchStatus(YSPUtils.getInstance().getBoolean("pushOpen", false));
        }
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onLogoutEvent.observe(this, o -> showDialogLogout());
        viewModel.uc.onClearCacheEvent.observe(this, o -> showDialogClearCache());
        viewModel.uc.onUpdateVersionEvent.observe(this, o -> showDialogVersion((VersionEntity)o));
    }

    /**
     * 退出登录
     */
    private void showDialogLogout() {
        YDialogUtil.getInstance().showConfirm(getContext(), getResources().getString(R.string.mine_set_logout_tip))
                .setOnConfirmListener(()->{
                    YLoginUtil.getInstance().logout(getContext());
                    YActivityUtil.getInstance().close(viewModel);
                });
    }
    /**
     * 清除缓存
     */
    private void showDialogClearCache() {
        YDialogUtil.getInstance().showConfirm(getContext(), getResources().getString(R.string.dialog_clear_memory_content))
                .setOnConfirmListener(()->{
                    DataCleanManager.clearAllCache(getContext());
                    setCacheUI();
                });
    }
    /**
     * 缓存UI
     */
    private void setCacheUI() {
        try {
            String resultCache = DataCleanManager.getTotalCacheSize(getContext());
            binding.tvCache.setContent(resultCache);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 版本更新
     */
    private void showDialogVersion(VersionEntity entity) {
        YDialogUtil.getInstance().showVersion(getContext(), entity, BuildConfig.VERSION_CODE);
    }
}
